package com.cinema.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cinema.bean.MovieCategory;
import com.cinema.bean.User;
import com.cinema.service.MovieCategoryService;
import com.cinema.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("movieCategory")
@CrossOrigin
public class MovieCategoryController {

    @Autowired
    private MovieCategoryService movieCategoryService;

    @GetMapping("list")
    public Result getList() {
        List<MovieCategory> list = movieCategoryService.list();
        return Result.success(list);
    }

    @GetMapping("page")
    public Result getPage(Page page, @RequestParam("searchText") String searchText){
        System.out.println(searchText);
        IPage<MovieCategory> result=null;
        if(searchText==null||searchText.trim().isEmpty()){
            result=movieCategoryService.page(page);
        }else {
            QueryWrapper wrapper=new QueryWrapper();
            wrapper.like("movie_category_name",searchText);
            result=movieCategoryService.page(page,wrapper);
        }
        return Result.success(result);
    }

    @PostMapping
    public Result add(@RequestBody MovieCategory movieCategory) {
        movieCategoryService.save(movieCategory);
        return Result.success("保存成功", movieCategory);
    }

    @DeleteMapping("/{movie_category_id}")
    public Result delete(@PathVariable("movie_category_id") Long movieCategoryId) {
        boolean b = movieCategoryService.removeById(movieCategoryId);
        if (b) {
            return Result.success("删除成功");
        } else {
            return Result.fail(201, "删除失败");
        }
    }

    @PutMapping
    public Result edit(@RequestBody MovieCategory movieCategory) {
        boolean b = movieCategoryService.updateById(movieCategory);
        if (b) {
            return Result.success("修改成功");
        } else {
            return Result.fail(201, "修改失败");
        }
    }
}